package com.earnings.okhttputils.utils.ui.activity.user;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;

import java.util.HashMap;
import java.util.Map;

/**编辑用户信息-设置支付密码
 */

public class UserEditPaypasswordSetOldActivity extends GodLeftHandBaseActivity implements View.OnClickListener {


    private Button sumbit;
    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_user_edit_paypsw_set;
    }

    @Override
    protected void init() {

        setTitle("支付密码设置");
        sumbit= (Button) findViewById( R.id.sumbit );
        sumbit.setOnClickListener(this);
        ((TextView) findViewById( R.id.info )).setText( "请输入原支付密码" );
        initCodeEdittext();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.sumbit) {
            bundleData.putString( "oldpwd",getPsw() );
            skipActivity( UserEditPaypasswordSetNewActivity.class );
            finish();

        }
    }

    public String getPsw(){
        String password="";
        for (int i = 1; i <=size ; i++) {
            int rid=getIdFromXML("code"+i);
            password+=((EditText)findViewById(rid)).getText().toString();
        }
        return password;
    }

    /*
初始化验证码监听
 */
    final int size=6;
    public void initCodeEdittext() {
        final Map<Integer, EditText> codeMap = new HashMap<>();
        for (int i = 1; i <= size; i++) {
            int rid = getIdFromXML( "code" + i );
            final EditText code = (EditText) findViewById( rid );
            final int num = i;
            if (num == 1) {
                code.clearFocus();
                code.setFocusable( true );
                code.setFocusableInTouchMode( true );
                code.requestFocus();
                getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE );
            }
            //code.setEnabled(false);
            codeMap.put( i, code );
            //监听输入
            code.addTextChangedListener( new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    code.clearFocus();
                    if (code.getText().toString().length() == 1 && num != size) {


                        //  code.setBackgroundResource(R.drawable.code_radius_false);
                        EditText nowEditText = codeMap.get( num + 1 );

                        nowEditText.setText( "" );
                        nowEditText.setFocusable( true );
                        nowEditText.requestFocus();
                        //  nowEditText.setBackgroundResource(R.drawable.code_radius_true);
                    } else if (num == size && code.getText().toString().length() != 0) {
                        sumbit.setBackgroundResource( R.drawable.btn_sumbit );
                        //checkode();
                    }
                }
            } );
            //监听删除
            code.setOnKeyListener( new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {

                    sumbit.setBackgroundResource( R.drawable.btn_sumbit_un );
                    if (keyCode == KeyEvent.KEYCODE_DEL
                            && event.getAction() == KeyEvent.ACTION_DOWN) {

                        if (num != 1) {

                            EditText nowEditText = codeMap.get( num - 1 );
                            code.clearFocus();
                            // code.setBackgroundResource(R.drawable.code_radius_false);
                            if (code.getText().toString().length() == 0) nowEditText.setText( "" );
                            nowEditText.setFocusable( true );
                            nowEditText.requestFocus();
                            //  nowEditText.setBackgroundResource(R.drawable.code_radius_true);
                        }
                        code.setText( "" );
                        return true;
                    } else {
                        // code.setText(event.getNumber());
                    }
                    return false;
                }
            } );

        }
    }
}
